package com.sinothk.software.utils;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import java.io.Serializable;

public class IntentUtil {

    private long lasttime;

    public IntentBuilder open(Ability currAbility, Class<?> targetAbilityClass) {
        return open(currAbility, targetAbilityClass, currAbility.getBundleName());
    }

    public IntentBuilder open(Ability currAbility, Class<?> targetAbilityClass, String bundleName) {
        return open(currAbility, targetAbilityClass, bundleName, "");
    }

    public IntentBuilder open(Ability currAbility, Class<?> gotoActivity, String bundleName, String deviceId) {
        return new IntentBuilder(currAbility, gotoActivity, bundleName, deviceId);
    }

    public class IntentBuilder {

        private boolean isFinish;
        //        private Bundle mDataBundle;
        private int mEnterAnimRes;
        private int mExitAnimRes;
        private int mFlags;
        private Ability currAbility;
        private Intent mIntent;
        private int mRequestCode = -999;
        private Class<?> mToActivity;

        IntentBuilder(Ability currAbility, Class<?> paramClass, String bundleName, String deviceId) {
            this.currAbility = currAbility;
            this.mToActivity = paramClass;
            this.mIntent = new Intent();

            // 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId(deviceId)
                    .withBundleName(bundleName)
                    .withAbilityName(paramClass.getName())
                    .build();
            // 把operation设置到intent中
            mIntent.setOperation(operation);
        }

        public IntentBuilder anim(int mEnterAnimRes, int mExitAnimRes) {
            this.mEnterAnimRes = mEnterAnimRes;
            this.mExitAnimRes = mExitAnimRes;
            return this;
        }

        public IntentBuilder finish(boolean isFinish) {
            this.isFinish = isFinish;
            return this;
        }

        public IntentBuilder putBooleanExtra(String key, boolean value) {
            this.mIntent.setParam(key, value);
            return this;
        }

//        public IntentBuilder putExtras(Bundle bundle) {
//            this.mDataBundle = bundle;
//            return this;
//        }

        public IntentBuilder putIntExtra(String key, int value) {
            this.mIntent.setParam(key, value);
            return this;
        }

        public IntentBuilder putSerializableExtra(String key, Serializable value) {
            this.mIntent.setParam(key, value);
            return this;
        }

        public IntentBuilder putStringExtra(String key, String value) {
            this.mIntent.setParam(key, value);
            return this;
        }

        public IntentBuilder requestCode(int requestCode) {
            this.mRequestCode = requestCode;
            return this;
        }

        public IntentBuilder setFlags(int flags) {
            this.mFlags = flags;
            return this;
        }

        public void start() {
            if (System.currentTimeMillis() - lasttime <= 500L)
                return;

            lasttime = System.currentTimeMillis();

//            if (this.mDataBundle != null)
//                this.mIntent.putExtras(this.mDataBundle);

//            if (this.mFlags != 0)
//                this.mIntent.setFlags(this.mFlags);

            if (mRequestCode == -999) {
                this.currAbility.startAbility(this.mIntent);
            } else {
                this.currAbility.startAbilityForResult(mIntent, mRequestCode);
            }

//            if ((this.mEnterAnimRes != 0) && (this.mExitAnimRes != 0)) {
//                currAbility.overridePendingTransition(this.mEnterAnimRes, this.mExitAnimRes);
//            } else {
////                currAbility.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
////                currAbility.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//            }

            if (this.isFinish){
                currAbility.terminateAbility();
            }

            //
//            mDataBundle = null;
            mIntent = null;
        }

//        public void startInFragment(Fragment fragment) {
//
//            if (System.currentTimeMillis() - lasttime <= 500L)
//                return;
//
//            lasttime = System.currentTimeMillis();
//
//            if (mDataBundle != null)
//                mIntent.putExtras(mDataBundle);
//
//            if (mFlags != 0)
//                mIntent.setFlags(mFlags);
//
//            if (mRequestCode == -999) {
//                fragment.startActivity(mIntent);
//            } else {
//                fragment.startActivityForResult(mIntent, mRequestCode);
//            }
//            //
//            mDataBundle = null;
//            mIntent = null;
//        }
    }
}
